package com.ld.security.server.admin.modules.ge.service.impl;


import com.ld.security.server.admin.modules.ge.entity.GeCompEntity;
import com.ld.security.server.admin.modules.ge.entity.GeCompTelEntity;
import com.ld.security.server.admin.modules.ge.service.IGeCompService;
import com.ld.security.server.admin.modules.ge.service.IGeCompTelService;
import com.ld.security.server.admin.modules.ge.vo.CompTreeVo;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import com.ld.security.common.mybatis.base.vo.NodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GeCompServiceImpl  extends ISuperServiceImpl<GeCompEntity>
implements
        IGeCompService {

        @Autowired
        IGeCompTelService iGeCompTelService;

        @Override
        public int save(GeCompEntity entity) {
                //新增才初始化组件
                if(StringUtil.isNull(entity.getId())){
                        GeCompTelEntity telEntity =  iGeCompTelService.getById(entity.getTelId());
                        ApiException.throw400(telEntity == null,"组件模板不能为空");
                        entity.setConfig(telEntity.getConfig());
                        entity.setCode(telEntity.getCode());
                }


                return super.save(entity);
        }


        @Override
        public List<NodeVo> getCompTree() {
                CompTreeVo vo = new CompTreeVo();
                this.proxyQuery(vo);
                return vo.getCompTree();
        }
}
